package com.wechatFriendExportTool.utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.util.List;

public class ExcelUtil {

    public static void addSheet(SXSSFWorkbook wb, String sheetName, List<String> headList, List<List<String>> contentList) {
        //开姑创滨Exce1文件//创建行索引
        int nRowIndex = 0;
//创建列索引
        int ncellIndex = 0;
//创建Sheet对象
        Sheet sheet = wb.createSheet(sheetName);
//创建标题
//创建行对象(索引从0开始)
        Row nRow = sheet.createRow(nRowIndex++);
//创建单元格祥式对象
        CellStyle style = wb.createCellStyle();
//8.创建字体对象
        Font font = wb.createFont();
//9.设置字体和其大小及效果
        font.setFontName("黑体");
//加粗
        font.setBold(true);
//10.设置样式
        style.setFont(font);
//横向居中
        style.setAlignment(HorizontalAlignment.CENTER);//纵向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
//设置自动换行
        style.setWrapText(true);
//设置背景颜色
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setFillForegroundColor(IndexedColors.LIME.index);
        //设费标题内容
        for (String head : headList) {
//创建列
            Cell cellNew = nRow.createCell(ncellIndex++);
            cellNew.setCellValue(head);
//为单元格应用样式
            cellNew.setCellStyle(style);
        }

        for (List<String> contents : contentList) {
            ncellIndex = 0;
            Row rowNew = sheet.createRow(nRowIndex++);
            for (String content : contents) {
                Cell cellNew = rowNew.createCell(ncellIndex++);
                cellNew.setCellValue(content);
            }
        }
    }
}
